//Date: 2023-11-12 22:37:37
//LastEditors: jie zhengjie_liang@qq.com
//LastEditTime: 2023-12-11 22:30:48
//FilePath: /photowall/pkg/main/service/guid_service.go

package service

import (
	"crypto/sha256"
	"encoding/hex"
	"math/rand"
	"photowall/pkg/log"
	"sync"
	"time"

	"github.com/bwmarrin/snowflake"
)

//	func getRandInt() {
//		var reader io.Reader
//		var randSeed big.Int
//		randSeed.SetInt64(1000)
//		rand.Int(reader, &randSeed)
//	}
func init() {
	testService()
}

func testService() {
	GetGuidForImage()
	GetGuidForSession(``)
}

var (
	nodeImage         *snowflake.Node
	nodeImageSyncOnce sync.Once
)

//description: 用于生成图片的GUID
//return {*}

func GetGuidForImage() string {
	nodeImageSyncOnce.Do(func() {
		newNode, err := snowflake.NewNode(1000)
		if err != nil {
			go log.GetMainLogger().Error(err.Error())
			return
		}

		nodeImage = newNode
	})
	id := nodeImage.Generate()

	return id.String()
}

var (
	nodeWechatUserID         *snowflake.Node
	nodeWechatUserIDSyncOnce sync.Once
)

//description: 用于生成微信用户User的GUID
//return {*}

func GetGuidForWechatUserID() string {
	nodeWechatUserIDSyncOnce.Do(func() {
		newNode, err := snowflake.NewNode(1010)
		if err != nil {
			go log.GetMainLogger().Error(err.Error())
			return
		}

		nodeWechatUserID = newNode
	})
	id := nodeWechatUserID.Generate()

	return id.String()
}

var (
	nodeAlbumID         *snowflake.Node
	nodeAlbumIDSyncOnce sync.Once
)

//description: 用于生成微信用户User的GUID
//return {*}

func GetGuidForAlbumID() string {
	nodeAlbumIDSyncOnce.Do(func() {
		newNode, err := snowflake.NewNode(1020)
		if err != nil {
			go log.GetMainLogger().Error(err.Error())
			return
		}

		nodeAlbumID = newNode
	})
	id := nodeAlbumID.Generate()

	return id.String()
}

var (
	nodePhotoID         *snowflake.Node
	nodePhotoIDSyncOnce sync.Once
)

//description: 用于生成微信用户User的GUID
//return {*}

func GetGuidForPhotoID() string {
	nodePhotoIDSyncOnce.Do(func() {
		newNode, err := snowflake.NewNode(1030)
		if err != nil {
			go log.GetMainLogger().Error(err.Error())
			return
		}

		nodePhotoID = newNode
	})
	id := nodePhotoID.Generate()

	return id.String()
}

var (
	nodeSession         *snowflake.Node
	nodeSessionSyncOnce sync.Once
)

//description: 用于生成session的GUID
//return {*}

func GetGuidForSession(userID string) string {
	nodeSessionSyncOnce.Do(func() {
		newNode, err := snowflake.NewNode(1040)
		if err != nil {
			log.GetMainLogger().Error(err.Error())
			return
		}

		nodeSession = newNode
	})
	id := nodeSession.Generate()

	// 使用哈希随机化
	hash := sha256.Sum256([]byte(id.String() + generateRandomString(10)))

	return hex.EncodeToString(hash[:])
}

var (
	rng         *rand.Rand
	rngSyncOnce sync.Once
	charset     = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789"
)

func generateRandomString(length int) string {
	rngSyncOnce.Do(func() {
		// 创建一个本地随机数生成器
		rng = rand.New(rand.NewSource(time.Now().UnixNano()))
	})

	result := make([]byte, length)
	for i := 0; i < length; i++ {
		randomIndex := rng.Intn(len(charset))
		result[i] = charset[randomIndex]
	}

	return string(result)
}
